package com.nnfe.fyd.feature.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.BaseActivity;
import com.nnfe.fyd.base.WebViewActivity;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.ad.AdBean;
import com.nnfe.fyd.feature.main.MainActivity;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.ImageUtil;
import com.nnfe.fyd.utils.SharePrefUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

public class SplashAdActivity extends BaseActivity {

    @Bind(R.id.start_pic)
    ImageView startPic;
    @Bind(R.id.btn_skip)
    TextView btnSkip;

    /**
     * 本地闪屏图资源id
     */
    protected int mChannelLogoId = 0;
    // 开屏广告跳转地址
    private String jump_url = "";
    // 跳转地址的标题
    private String jump_title = "";


    public static void launch(Activity context) {
        Intent intent = new Intent(context, SplashAdActivity.class);
        context.startActivity(intent);
        context.overridePendingTransition(android.R.anim.fade_in,
                android.R.anim.fade_out);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // 全屏展示 覆盖状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getStartPic();
        changeButtonText();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public Fragment getContentFragment() {
        return null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_splashad;
    }

    // 倒计时开始时间
    private int COUNT_DOWN = 5;

    private void changeButtonText() {
        btnSkip.setText(COUNT_DOWN + "秒");
        handler.sendEmptyMessageDelayed(0, 1000);
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (COUNT_DOWN == 0) {
                // 第一次启动app，跳转到不可描述页
                if (SharePrefUtil.getBooleanWithoutUser(mContext, ShareKey.isFirstStart, true)) {
                    SplashActivity.launch(mContext);
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                } else {
                    MainActivity.launch(mContext);
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                }

            } else {
                COUNT_DOWN--;
                Log.e("xyd", "倒计时： " + COUNT_DOWN);
                changeButtonText();
            }
        }
    };

    /**
     * 获取开机引导图并缓存
     */
    public void getStartPic() {
        String url = SharePrefUtil.getString(mContext, "ADURL");
        if (!TextUtils.isEmpty(url)) {
            ImageUtil.displayImage(url, startPic);

        } else {
            startPic.setBackgroundResource(R.drawable.sp);
        }

        service.findByType("2").enqueue(new CallbackImpl<List<AdBean>>() {
            @Override
            public void responseSuccess(Call<HttpResult<List<AdBean>>> call, Response<HttpResult<List<AdBean>>> response) {

                if (response.body().status == 0) {
                    List<AdBean> list = response.body().data;
                    if (list != null && list.size() > 0) {
                        String img = list.get(0).imgUrl;
                        SharePrefUtil.saveString(mContext, "ADURL", img);
                        ImageUtil.displayImage(img, startPic);

                        String title = list.get(0).title;
                        String url = list.get(0).jumpUrl;

                        jump_title = list.get(0).title;
                        jump_url = list.get(0).jumpUrl;


                        SharePrefUtil.saveString(mContext, "ADU", url);
                        SharePrefUtil.saveString(mContext, "ADT", title);

                    }
                }

            }
        });

    }

    @OnClick({R.id.start_pic, R.id.btn_skip})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_pic:

                // 第一次启动app，点击广告页，
                // 如果跳转连接无内容，则禁止跳转
                if (TextUtils.isEmpty(jump_url)) {
                    return;
                } else {
                    // 跳转页有内容，进行跳转
                    // 终止倒计时，先跳转到主页，后跳转到不可描述页，在关闭本页
                    handler.removeMessages(0);
                    if (SharePrefUtil.getBooleanWithoutUser(mContext, ShareKey.isFirstStart, true)) {
                        SplashActivity.launch(mContext);
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    } else {
                        MainActivity.launch(mContext);
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    }
                    WebViewActivity.lauch(mContext, jump_title, jump_url);
                    finish();
                }

                break;
            case R.id.btn_skip:

                // 点击跳过，倒计时时间置 0 ，并发送马上执行的 message
                jumpToMainPage();

                break;
        }
    }

    /**
     * 跳过倒计时
     */
    private void jumpToMainPage() {
        COUNT_DOWN = 0;
        handler.removeMessages(0);
        handler.sendEmptyMessageDelayed(0, 0);
    }
}
